﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Service
{
    public partial class UserFavoriteCommand : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["userid"] != null && Request["serviceid"] != null)
            {
                string serviceid = Request["serviceid"].ToString();
                string userid = Request["userid"].ToString();
                int command = 0;
                try
                {
                    command = int.Parse(Request["command"] ?? "0");//默认为查询操作
                }
                catch
                {
                    Response.Write("113"); //无效的整型数值
                    return;
                }

                string secretkey=Request["secretkey"]??"";

                if (!IsValidKey(secretkey, serviceid, userid, command))
                {
                    Response.Write("112");//密钥不正确 
                    return;
                }



                Model.ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(serviceid);
                if (service == null)
                {
                    Response.Write("300");//无效的服务
                    return;
                }
                Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetail(userid);
                if (user == null)
                {
                    Response.Write("301");//无效的用户
                    return;
                }
                

                Model.Favorite favorite = new Model.Favorite();
                favorite.ServiceID = serviceid;
                favorite.UserID = userid;
                if (command == 0)
                {
                    if (BLL.FavoriteBLL.Instance.Contain(serviceid, userid))
                    {
                        Response.Write("103");//服务已被收藏
                    }
                    else
                    {
                        Response.Write("104");//服务未被收藏
                    }
                }
                else if (command == 1)
                {
                    if (BLL.FavoriteBLL.Instance.Contain(serviceid, userid))
                    {
                        Response.Write("103");//服务已被收藏
                    }
                    else
                    {
                        favorite.FavoriteID = Guid.NewGuid().ToString();
                        BLL.FavoriteBLL.Instance.Add(favorite);

                        Response.Write("100");//收藏成功
                    }
                }
                else if (command == 2)
                {
                    if (!BLL.FavoriteBLL.Instance.Contain(serviceid, userid))
                    {
                        Response.Write("104");//未收藏该服务
                    }
                    else
                    {
                        BLL.FavoriteBLL.Instance.Delete(favorite);
                        Response.Write("102");//取消收藏成功
                    }
                }
            }
            else
            {
                Response.Write("111");//传入参数不正确
            }
        }

        private bool IsValidKey(string secretkey, string serviceid, string userid, int command)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = base.EncryptMD5(public_key + current_day + secretkey + userid + command.ToString()).Substring(6, 22);
                    return secretkey == finalkey;
                }
            }
            else
            {
                return true;
            }
        }
    }
}